<?php
// adapted from: http://bakery.cakephp.org/articles/view/country-select-list-helper
$countries = array(
	'US' => 'United States',
	'CA' => 'Canada',
	'AF' => 'Afganistan',
	'AL' => 'Albania',
	'DZ' => 'Algeria',
	'AS' => 'American Samoa',
	'AD' => 'Andorra', 
	'AO' => 'Angola',
	'AI' => 'Anguilla',
	'AQ' => 'Antarctica',
	'AG' => 'Antigua and Barbuda', 
	'AR' => 'Argentina', 
	'AM' => 'Armenia', 
	'AW' => 'Aruba', 
	'AU' => 'Australia', 
	'AT' => 'Austria', 
	'AZ' => 'Azerbaijan',
	'BS' => 'Bahamas', 
	'BH' => 'Bahrain', 
	'BD' => 'Bangladesh',
	'BB' => 'Barbados',
	'BY' => 'Belarus', 
	'BE' => 'Belgium', 
	'BZ' => 'Belize',
	'BJ' => 'Benin', 
	'BM' => 'Bermuda', 
	'BT' => 'Bhutan',
	'BO' => 'Bolivia', 
	'BA' => 'Bosnia and Herzegowina',
	'BW' => 'Botswana',
	'BV' => 'Bouvet Island', 
	'BR' => 'Brazil',
	'IO' => 'British Indian Ocean Territory',
	'BN' => 'Brunei Darussalam', 
	'BG' => 'Bulgaria',
	'BF' => 'Burkina Faso',
	'BI' => 'Burundi', 
	'KH' => 'Cambodia',
	'CM' => 'Cameroon',
	'CV' => 'Cape Verde',
	'KY' => 'Cayman Islands',
	'CF' => 'Central African Republic',
	'TD' => 'Chad',
	'CL' => 'Chile', 
	'CN' => 'China',
	'CX' => 'Christmas Island',    
	'CC' => 'Cocos (Keeling) Islands', 
	'CO' => 'Colombia',
	'KM' => 'Comoros', 
	'CG' => 'Congo', 
	'CD' => 'Congo, the Democratic Republic of the', 
	'CK' => 'Cook Islands',
	'CR' => 'Costa Rica',
	'CI' => 'Cote d\'Ivoire', 
	'HR' => 'Croatia (Hrvatska)',
	'CU' => 'Cuba',
	'CY' => 'Cyprus',
	'CZ' => 'Czech Republic',
	'DK' => 'Denmark', 
	'DJ' => 'Djibouti',
	'DM' => 'Dominica',
	'DO' => 'Dominican Republic',
	'TP' => 'East Timor',
	'EC' => 'Ecuador', 
	'EG' => 'Egypt', 
	'SV' => 'El Salvador', 
	'GQ' => 'Equatorial Guinea', 
	'ER' => 'Eritrea', 
	'EE' => 'Estonia', 
	'ET' => 'Ethiopia',
	'FK' => 'Falkland Islands (Malvinas)', 
	'FO' => 'Faroe Islands', 
	'FJ' => 'Fiji',
	'FI' => 'Finland',
	'FR' => 'France',
	'FX' => 'France, Metropolitan',
	'GF' => 'French Guiana', 
	'PF' => 'French Polynesia',
	'TF' => 'French Southern Territories', 
	'GA' => 'Gabon', 
	'GM' => 'Gambia',
	'GE' => 'Georgia', 
	'DE' => 'Germany', 
	'GH' => 'Ghana', 
	'GI' => 'Gibraltar', 
	'GR' => 'Greece',
	'GL' => 'Greenland', 
	'GD' => 'Grenada', 
	'GP' => 'Guadeloupe',
	'GU' => 'Guam',
	'GT' => 'Guatemala', 
	'GN' => 'Guinea',
	'GW' => 'Guinea-Bissau', 
	'GY' => 'Guyana',
	'HT' => 'Haiti', 
	'HM' => 'Heard and Mc Donald Islands', 
	'VA' => 'Holy See (Vatican City State)', 
	'HN' => 'Honduras',
	'HK' => 'Hong Kong', 
	'HU' => 'Hungary', 
	'IS' => 'Iceland', 
	'IN' => 'India', 
	'ID' => 'Indonesia', 
	'IR' => 'Iran (Islamic Republic of)',
	'IQ' => 'Iraq',
	'IE' => 'Ireland', 
	'IL' => 'Israel',
	'IT' => 'Italy', 
	'JM' => 'Jamaica', 
	'JP' => 'Japan',
	'JO' => 'Jordan',
	'KZ' => 'Kazakhstan',
	'KE' => 'Kenya', 
	'KI' => 'Kiribati',
	'KP' => 'Korea, Democratic People\'s Republic of',
	'KR' => 'Korea, Republic of',
	'KW' => 'Kuwait',
	'KG' => 'Kyrgyzstan',
	'LA' => 'Lao People\'s Democratic Republic',
	'LV' => 'Latvia',
	'LB' => 'Lebanon',
	'LS' => 'Lesotho', 
	'LR' => 'Liberia', 
	'LY' => 'Libyan Arab Jamahiriya',
	'LI' => 'Liechtenstein', 
	'LT' => 'Lithuania',
	'LU' => 'Luxembourg',
	'MO' => 'Macau', 
	'MK' => 'Macedonia, The Former Yugoslav Republic of',
	'MG' => 'Madagascar',
	'MW' => 'Malawi',
	'MY' => 'Malaysia',
	'MV' => 'Maldives',
	'ML' => 'Mali',
	'MT' => 'Malta',
	'MH' => 'Marshall Islands',
	'MQ' => 'Martinique',
	'MR' => 'Mauritania',
	'MU' => 'Mauritius',
	'YT' => 'Mayotte', 
	'MX' => 'Mexico',
	'FM' => 'Micronesia, Federated States of',
	'MD' => 'Moldova, Republic of',
	'MC' => 'Monaco',
	'MN' => 'Mongolia',
	'MS' => 'Montserrat',
	'MA' => 'Morocco',
	'MZ' => 'Mozambique',
	'MM' => 'Myanmar',
	'NA' => 'Namibia',
	'NR' => 'Nauru', 
	'NP' => 'Nepal', 
	'NL' => 'Netherlands',
	'AN' => 'Netherlands Antilles',
	'NC' => 'New Caledonia',
	'NZ' => 'New Zealand', 
	'NI' => 'Nicaragua', 
	'NE' => 'Niger', 
	'NG' => 'Nigeria', 
	'NU' => 'Niue',
	'NF' => 'Norfolk Island',
	'MP' => 'Northern Mariana Islands',
	'NO' => 'Norway',
	'OM' => 'Oman',
	'PK' => 'Pakistan',
	'PW' => 'Palau',
	'PA' => 'Panama',
	'PG' => 'Papua New Guinea',
	'PY' => 'Paraguay',
	'PE' => 'Peru',
	'PH' => 'Philippines',
	'PN' => 'Pitcairn',
	'PL' => 'Poland',
	'PT' => 'Portugal',
	'PR' => 'Puerto Rico',
	'QA' => 'Qatar',
	'RE' => 'Reunion',
	'RO' => 'Romania',
	'RU' => 'Russian Federation',
	'RW' => 'Rwanda',
	'KN' => 'Saint Kitts and Nevis', 
	'LC' => 'Saint LUCIA', 
	'VC' => 'Saint Vincent and the Grenadines',
	'WS' => 'Samoa', 
	'SM' => 'San Marino',
	'ST' => 'Sao Tome and Principe',
	'SA' => 'Saudi Arabia',
	'SN' => 'Senegal',
	'SC' => 'Seychelles',
	'SL' => 'Sierra Leone',
	'SG' => 'Singapore', 
	'SK' => 'Slovakia (Slovak Republic)',
	'SI' => 'Slovenia',
	'SB' => 'Solomon Islands',
	'SO' => 'Somalia', 
	'ZA' => 'South Africa',
	'GS' => 'South Georgia and the South Sandwich Islands',
	'ES' => 'Spain',
	'LK' => 'Sri Lanka',
	'SH' => 'St. Helena',
	'PM' => 'St. Pierre and Miquelon', 
	'SD' => 'Sudan', 
	'SR' => 'Suriname',
	'SJ' => 'Svalbard and Jan Mayen Islands',
	'SZ' => 'Swaziland', 
	'SE' => 'Sweden',
	'CH' => 'Switzerland', 
	'SY' => 'Syrian Arab Republic',
	'TW' => 'Taiwan, Province of China',
	'TJ' => 'Tajikistan',
	'TZ' => 'Tanzania, United Republic of',
	'TH' => 'Thailand',
	'TG' => 'Togo',
	'TK' => 'Tokelau',
	'TO' => 'Tonga', 
	'TT' => 'Trinidad and Tobago', 
	'TN' => 'Tunisia', 
	'TR' => 'Turkey',
	'TM' => 'Turkmenistan',
	'TC' => 'Turks and Caicos Islands',
	'TV' => 'Tuvalu',
	'UG' => 'Uganda',
	'UA' => 'Ukraine',
	'AE' => 'United Arab Emirates',
	'GB' => 'United Kingdom',
	'UM' => 'United States Minor Outlying Islands',
	'UY' => 'Uruguay', 
	'UZ' => 'Uzbekistan',
	'VU' => 'Vanuatu', 
	'VE' => 'Venezuela',
	'VN' => 'Viet Nam',
	'VG' => 'Virgin Islands (British)',
	'VI' => 'Virgin Islands (U.S.)', 
	'WF' => 'Wallis and Futuna Islands', 
	'EH' => 'Western Sahara',
	'YE' => 'Yemen', 
	'YU' => 'Yugoslavia',
	'ZM' => 'Zambia',
	'ZW' => 'Zimbabwe'
);

$type_empty = $this->Content_model->is_content_type_empty($content_type);
if (!isset($is_edit)) $is_edit = false;
?>
<form class="<?php if ($is_edit) echo 'edit_entry'; ?>" action="/content/<?php echo $is_edit ? 'update' : 'create'; ?>" method="post" onsubmit="return Entry.validateFields();" enctype="multipart/form-data">
	<input type="hidden" name="bricabox_id" value="<?php echo $bricabox_id; ?>" />
	<input type="hidden" name="type_id" value="<?php echo $type_id; ?>" />
	<?php if ($is_edit): ?>
	<input type="hidden" name="content_id" value="<?php echo $content->id; ?>" />
	<input type="hidden" name="content[user_id]" value="<?php echo $content->user_id; ?>" />
	<?php endif; ?>
	<fieldset>
		<div class="step">
			<span class="step"><?php echo $is_only_type ? 1 : 2; ?>.</span> Give this <?php echo strtolower($content_type->name); ?> a name.
		</div>
		
		<div>
			<input type="text" id="content_field1_value" style="width: 98%;" name="content[field1_value]" value="<?php if ($is_edit) echo form_prep($content->field1_value); ?>" autocomplete="off" <?php if (!$is_edit): ?>onchange="$('#content_field1_value').suggest('/bricabox/<?php echo $bricabox_id; ?>/title_results');" onfocus="$('#content_field1_value').suggest('/bricabox/<?php echo $bricabox_id; ?>/title_results');"<?php endif; ?> />
		</div>
		
		<?php if (!$type_empty): ?>
		<div class="step">
			<span class="step"><?php echo $is_only_type ? 2 : 3; ?>.</span> Enter <?php echo field_list_to_string($content_type); ?> for this <?php echo strtolower($content_type->name); ?>.
		</div>
		
		<?php if ($content_type->input_use_url): ?>
		<div>
			<label for="entry_url"><?php echo $content_type->label_url; ?>:</label><br />
			<input type="text" id="entry_url" name="content[data_url]" value="<?php if ($is_edit) echo $content->data_url; ?>" />
		</div>
		<?php endif; ?>
		
		<?php if ($content_type->input_use_address): ?>
		<div>
			<label for="entry_addr_1">Address 1:</label><br />
			<input type="text" id="entry_addr_1" name="content[data_addr_str1]" value="<?php if ($is_edit) echo $content->data_addr_str1; ?>" />
		</div>
		
		<div>
			<label for="entry_addr_2">Address 2:</label><br />
			<input type="text" id="entry_addr_2" name="content[data_addr_str2]" value="<?php if ($is_edit) echo $content->data_addr_str2; ?>" />
		</div>
		
		<div class="three_col">
			<div class="third small">
				<label for="entry_city">City:</label><br />
				<input type="text" id="entry_city" name="content[data_addr_city]" value="<?php if ($is_edit) echo $content->data_addr_city; ?>" />
			</div>
		
			<div class="third small">
				<label for="entry_state">State:</label><br />
				<input type="text" id="entry_state" name="content[data_addr_state]" value="<?php if ($is_edit) echo $content->data_addr_state; ?>" />
			</div>
		
			<div class="third small">
				<label for="entry_postal">Postal code:</label><br />
				<input type="text" id="entry_postal" name="content[data_addr_postal]" value="<?php if ($is_edit) echo $content->data_addr_postal; ?>" />
			</div>
		</div>
		
		<div>
			<label for="entry_country">Country:</label><br />
			<select id="entry_country" name="content[data_addr_country]">
				<option value="" selected="selected"></option>
				<?php foreach ($countries as $code => $label): ?>
				<option value="<?php echo $label; ?>"<?php if ($is_edit && $label == $content->data_addr_country) echo ' selected="selected"'; ?>><?php echo $label; ?></option>
				<?php endforeach; ?>
			</select>
		</div>
		<?php endif; ?>
		
		<?php if ($content_type->input_use_subtitle): ?>
		<div>
			<label for="entry_subtitle"><?php echo $content_type->label_subtitle; ?>:</label><br />
			<textarea id="entry_subtitle" name="content[data_subtitle]" rows="8" cols="35" style="width: 98%;"><?php if ($is_edit) echo $content->data_subtitle; ?></textarea>
		</div>
		<?php endif; ?>
		
		<?php if ($content_type->input_use_file_upload): ?>
		<div>
			<label for="file_upload"><?php echo $content_type->label_file_upload; ?>:</label><br />
			<input type="file" name="content[data_file_upload]" id="file_upload" />
		</div>
		<?php endif; ?>
		
		<?php if ($content_type->input_use_datetime): ?>
			
		<?php
		$content_dt = array();
		if ($is_edit)
		{
			$mkt = strtotime($content->data_datetime ? $content->data_datetime : mysql_now());
		}
		else
		{
			$mkt = strtotime(mysql_now());
		}
		$content_dt['year'] 	= date('Y', $mkt);
		$content_dt['month'] 	= date('m', $mkt);
		$content_dt['day'] 		= date('d', $mkt);
		$content_dt['hour'] 	= $is_edit ? date('H', $mkt) : -1; 
		$content_dt['min'] 		= $is_edit ? date('i', $mkt) : -1;
		?>
			
		<div>
			<label for="entry_datetime"><?php echo $content_type->label_datetime; ?>:</label><br />

			<select name="content[data_datetime][month]">
				<?php for ($dm=1;$dm<=12;$dm++): ?><option <?php if ($content_dt['month'] == $dm) echo 'selected="selected"'; ?> value="<?php echo $dm; ?>"><?php echo $dm; ?></option><?php endfor; ?>
			</select>
			/
			<select name="content[data_datetime][day]">
				<?php for ($dm=1;$dm<=31;$dm++): ?><option <?php if ($content_dt['day'] == $dm) echo 'selected="selected"'; ?> value="<?php echo $dm; ?>"><?php echo $dm; ?></option><?php endfor; ?>
			</select>
			/
			<select name="content[data_datetime][year]">
				<?php for ($dm=1901;$dm<=2030;$dm++): ?><option <?php if ($content_dt['year'] == $dm) echo 'selected="selected"'; ?> value="<?php echo $dm; ?>"><?php echo $dm; ?></option><?php endfor; ?>
			</select>
			<span class="datetime_hours">at</span>
			<select class="datetime_hours" name="content[data_datetime][hour]">
				<option value="">Hour</option>
				<?php for ($dm=1;$dm<=12;$dm++): ?><option <?php if ($content_dt['hour'] == $dm || intval($content_dt['hour'] - 12) == $dm) echo 'selected="selected"'; ?> value="<?php echo $dm; ?>"><?php echo $dm; ?></option><?php endfor; ?>
			</select>
			<span class="datetime_hours">:</span>
			<select class="datetime_hours" name="content[data_datetime][min]">
				<option value="">Minute</option>
				<?php for ($dm=0;$dm<=45;$dm+=15): ?><option <?php if ($content_dt['hour'] > 0 && str_pad($content_dt['min'], 2, '0') == $dm) echo 'selected="selected"'; ?> value="<?php echo $dm; ?>"><?php echo str_pad($dm, 2, '0'); ?></option><?php endfor; ?>
			</select>
			&nbsp;
			<select class="datetime_hours" name="content[data_datetime][ampm]">
				<option value="">AM/PM</option>
				<option <?php if ($content_dt['hour'] >= 1 && $content_dt['hour'] < 13) echo 'selected="selected"'; ?> value="0">AM</option>
				<option <?php if ($content_dt['hour'] >= 13) echo 'selected="selected"'; ?> value="1">PM</option>
			</select>
		</div>
		<?php endif; ?>

		<?php endif; ?>
		
		
		<div class="step small" id="entry_avatar_link">
			<a href="#" onclick="return Entry.showChooseAvatar();">Choose an optional avatar for this <?php echo strtolower($content_type->name); ?>&hellip;</a>
		</div>
		
		<div id="entry_avatar" style="display: none;">
			<div class="step">
				<span class="step"><?php $i = $is_only_type ? -1 : 0; echo $type_empty ? (3 + $i) : (4 + $i); ?>.</span> Choose an optional avatar for this <?php echo strtolower($content_type->name); ?>.
			</div>

			<div>
				<input type="file" name="content[content_image]" id="content_image" />
			</div>
		</div>
		
		<div class="controls">
			<input type="submit" value="Okay, <?php echo $is_edit ? 'update this' : 'save this new'; ?> <?php echo strtolower($content_type->name); ?>!" />
			<?php if ($is_edit): ?>
			<a class="delete" href="/content/<?php echo $content->id; ?>/destroy" onclick="return confirm('Are you sure? THIS IS PERMANENT AND CANNOT BE UNDONE.');">Remove this entry</a>
			or <a href="<?php echo content_url($content); ?>">cancel</a>
			<?php endif; ?>
		</div>
	</fieldset>
</form>

<script type="text/javascript" charset="utf-8">$('#content_field1_value').focus();</script>